class WelcomeController < ApplicationController
  before_filter :authorize, :only => [ :logout ]

  # POST login => authenticate
  # GET login => display login page 
  def login
    if request.post?
      session[:user_id] = nil
      user = User.authenticate(params[:name])
      if user
        session[:user_id] = user.id
        redirect_to(:controller => "welcome", :action => "index")
      else
        flash[:notice] = "Invalid user"
      end
    end
  end

  # logout
  def logout
    session[:user_id] = nil
    redirect_to(:action => "login")
  end

  def index
    if @current_user
      @time_delay = Time.now - 7.days
      @projects = @current_user.projects
      @owned_issues = @current_user.adoptions.recently_assigned.collect( &:issue ).select( &:open? )
      @recent_issues = @current_user.adopted_issues.recent
      @current_user.subscribed_issues.recent.each do |i|
        @recent_issues << i unless @recent_issues.include? i
      end
      @recent_issues.sort! { |a,b| a.updated_at <=> b.updated_at }
      @has_feed = true
    end

    respond_to do |format|
      format.html
      format.rss {
        render :layout => false
      }
    end
  end

  def about
  end

  def faq
  end

end
